package sanshui.system.trade.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

@Slf4j
public class CommonHttpUtil {
    public static String post(RestTemplate restTemplate, String url, String jsonStr){
        try {
            log.info("发起POST请求， url:{}, jsonStr:{}", url, jsonStr);
            //1.请求头
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);

            //2.请求体
            HttpEntity<String> httpEntity = new HttpEntity<>(jsonStr, httpHeaders);

            ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
            log.info("status:{}, body:{}", response.getStatusCodeValue(), response.getBody());
            if (response.getStatusCode() == HttpStatus.OK){
                return response.getBody();
            }
            return "";
        }catch (ResourceAccessException e){
            log.warn("没有找到回测报告服务");
        }catch (Exception e){
            log.error("回测报告服务异常", e);
        }
        return "";
    }
}
